import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store/index'
// 引入iview组件库和样式库
import './plugins/iview'
import 'view-design/dist/styles/iview.css'
// 引入全局样式
import './assets/css/global.css'
// 导入axios
import axios from 'axios'
// 涌入mavon-editoe
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'

// 请求拦截器，在所有调用axios前进入拦截器
axios.interceptors.request.use(config => {
  // 需要登录的请求将token信息存入请求头中
  config.headers.lhamster_identity_info = window.localStorage.getItem('lhamster_identity_info')
  // 指定为前台请求
  config.headers.type = 'front'
  return config
})

// 响应拦截器
axios.interceptors.response.use(response => {
  // 检验token是否过期，若过期则重新登录
  if (response.data.code === 4001 || response.data.code === 2001 || response.data.code === 4002) {
    // 清除本地token
    window.localStorage.removeItem('lhamster_identity_info')
    // 清除用户信息
    window.localStorage.removeItem('nickname')
    window.localStorage.removeItem('headpicture')
    location.reload()
  } else {
    return response
  }
})

axios.defaults.baseURL = 'http://www.lhamster.cn:8080/edwardBlog/'
Vue.prototype.$http = axios
Vue.config.productionTip = false
Vue.use(mavonEditor)
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
